<template>
  <div class="card-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form
          ref="form"
          :inline="true"
          label-width="80px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item label="项目名称">
            <el-input v-model="queryForm.projectName" placeholder="项目名称" />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-row :gutter="20">
      <el-col
        v-for="(item, index) in list"
        :key="index"
        :lg="8"
        :md="8"
        :sm="8"
        :xl="6"
        :xs="24"
      >
        <el-card
          :body-style="{ padding: '0px', position: 'static' }"
          shadow="hover"
        >
          <div class="card-title">
            {{ item.projectName }}
          </div>
          <div class="card-description">{{ item.projectDescription }}</div>
          <div class="card-datetime">{{ item.createTime }}</div>
          <div class="card-operate">
            <el-link class="delete" type="danger" @click="handleDelete(item)">
              删除
            </el-link>
            <el-link
              class="recover"
              type="success"
              @click="handleReduction(item)"
            >
              还原
            </el-link>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-pagination
      background
      :current-page="queryForm.pageIndex"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import {
    QueryRecycleProjectInfo,
    SysDelete,
    Reduction,
  } from '@/api/projectConfig/projectInfo'

  export default {
    name: 'CustomTable',
    components: {},
    data() {
      return {
        border: true,
        height: this.$baseTableHeight(1),
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageIndex: 1,
          pageSize: 20,
          projectName: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      // 删除项目信息
      handleDelete(project) {
        this.$baseConfirm(
          '你确定要删除【' +
            project.projectName +
            '】吗？删除后不可还原，请谨慎处理！',
          null,
          async () => {
            SysDelete({ ID: project.id }).then((res) => {
              if (res.data) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          }
        )
      },
      // 还原项目信息
      handleReduction(project) {
        this.$baseConfirm(
          '你确定要还原【' + project.projectName + '】吗',
          null,
          async () => {
            Reduction({ ID: project.id }).then((res) => {
              if (res.data) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          }
        )
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageIndex = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageIndex = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        QueryRecycleProjectInfo(this.queryForm).then((res) => {
          if (res.data === null || res.data === undefined) {
            this.$baseMessage(res.msg, 'error')
          } else {
            this.list = res.data.list
            this.total = res.data.total
          }
        })
        this.listLoading = false
      },
    },
  }
</script>

<style lang="scss" scoped>
  .card-title,
  .card-description,
  .card-datetime,
  .card-operate {
    margin-top: 6px;
    margin-bottom: 6px;
    margin-left: 10px;
    margin-right: 10px;
  }

  .card-description,
  .card-datetime {
    font-size: 14px;
    color: rgb(80, 80, 80);
  }

  .card-title {
    font-size: 16px;
    font-weight: bolder;
  }

  .card-description {
    height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .recover {
    margin-left: 10px;
  }
</style>
